/*
 * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND 
 * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
 * CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
 * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
 * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
 * 
 * Copyright (C) SEMTECH S.A.
 */
/*! 
 * \file       spi.c
 * \brief      SPI hardware driver
 *
 * \version    1.0
 * \date       Feb 12 2010
 * \author     Miguel Luis
 */
#include <stdint.h>
#if ( STM32F4XX )
#include "stm32f4xx_spi.h"
#elif defined( STM32F2XX )
#include "stm32f2xx_spi.h"
#elif defined( STM32F1XX )
#include "stm32f10x_spi.h"
#else
#include "stm32l1xx_spi.h"
#endif
#include "spi.h"

//#include "stm32f10x_spi.h"
#include "sx1276_Gpio.h"

void SpiInit( void )
{
  SPI_InitTypeDef SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	if(HAL_SPI == SPI1)
	{
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	}
	else if(HAL_SPI == SPI2)
	{
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
	}
	else if(HAL_SPI == SPI3)
	{
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
	}

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(HAL_PORT_CLOCK, ENABLE);

	GPIO_InitStructure.GPIO_Pin = HAL_PIN_MISO | HAL_PIN_MOSI | HAL_PIN_SCK;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_Init(HAL_PORT_SPI, &GPIO_InitStructure);

	//SPI_SSOutputCmd(HAL_SPI, ENABLE);
	GPIO_InitStructure.GPIO_Pin = HAL_PIN_SS;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(HAL_PORT_SS, &GPIO_InitStructure);
	GPIO_SetBits(HAL_PORT_SS, HAL_PIN_SS);

	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	if(HAL_SPI == SPI1)
	{
		SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
	}
	else
	{
		SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
	}

	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(HAL_SPI, &SPI_InitStructure);

	SPI_Cmd(HAL_SPI, ENABLE);
}

uint8_t SpiInOut( uint8_t data )
{
    u16 poll_counter = 1000;

	/* Loop while DR register in not emplty */
	while((SPI_I2S_GetFlagStatus(HAL_SPI, SPI_I2S_FLAG_TXE) == RESET) && (poll_counter--));

	/* Send byte through the SPI peripheral */
	SPI_I2S_SendData(HAL_SPI, data);

	poll_counter = 1000;

	/* Wait to receive a byte */
	while(SPI_I2S_GetFlagStatus(HAL_SPI, SPI_I2S_FLAG_RXNE) == RESET && (poll_counter--));

	/* Return the byte read from the SPI bus */
	return SPI_I2S_ReceiveData(HAL_SPI);
}

